package org.sws.domain.activity.service.quota.rule.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.sws.domain.activity.model.entity.ActivityCountEntity;
import org.sws.domain.activity.model.entity.ActivityEntity;
import org.sws.domain.activity.model.entity.ActivitySkuEntity;
import org.sws.domain.activity.model.valobj.ActivityStateVO;
import org.sws.domain.activity.service.quota.rule.AbstractActivityChain;
import org.sws.types.enums.ResponseCode;
import org.sws.types.exception.AppException;

import java.util.Date;

/**
 * @Author sws
 * @Date 2025/3/19 09:34
 * @description: 活动规则过滤【日期、状态】
 */
@Slf4j
@Component("activity_base_action")
public class ActivityBaseAction extends AbstractActivityChain {

    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息【有效期、状态】校验开始。");
        if (activityEntity.getState().equals(ActivityStateVO.close)) {
            log.info("活动责任链校验【当前活动未开始】");
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        Date currentDate = new Date();
        if(activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)){
            log.info("活动责任链校验【当前活动未开始】");
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        // 校验；活动sku库存 「剩余库存从缓存获取的」
        if (activitySkuEntity.getStockCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACTIVITY_COUNT_OVER.getCode(), ResponseCode.ACTIVITY_COUNT_OVER.getInfo());
        }
        log.info("活动责任链-基础信息【有效期、状态】校验通过。");
        return next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
